/*
Copyright (C) 2010 
Author		  Oliver Schenk
Contact		  Oliver.Schenk76@gmail.com

This program is free software; you can redistribute
it and/or modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 3 of the License,
or (at your option) any later version.
This program is distributed in the hope that it will be useful, 
but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 
or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with this program; 
if not, see <http://www.gnu.org/licenses/>.
*/

package SelfBookingSystem;

import java.io.IOException;
import java.net.URL;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.gdata.client.calendar.CalendarService;
import com.google.gdata.data.PlainTextConstruct;
import com.google.gdata.data.calendar.CalendarEventEntry;
import com.google.gdata.data.extensions.When;
import com.google.gdata.util.AuthenticationException;
import com.google.gdata.util.ServiceException;

public class BookAppointment extends HttpServlet {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	String GoogleCalendardURL = new String();
	String EmailAddress = new String();
	String Password = new String();
	
	public void doPost(HttpServletRequest req, HttpServletResponse resp)
       throws IOException {

		String AppID = new String();
		String tmp = "";
		if (req.getParameter("AppID").compareTo(tmp) > 0){
			AppID = req.getParameter("AppID");
		}else{
			resp.sendRedirect("/Error.jsp");
			//AppID = "xxx"; // Dummy value
		}

		   
		    // Get Parameters from HTML form
	        String myDate= req.getParameter("Date");
	        String myTime = req.getParameter("Time");
	        String myFirstname = req.getParameter("Firstname");
	        String mySurname = req.getParameter("Surname");
	        String myEmail = req.getParameter("Email");
	        String myPhone = req.getParameter("Phone");
	        
	        DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm");
	        Date starttime = null;
	        Date endtime = null;
	        try {
				starttime = df.parse(myDate + " " + myTime);
			} catch (ParseException e) {
				e.printStackTrace();
			}
	        
			long oneHour = 60 * 60 * 1000;
		    long StartTimeInMs = starttime.getTime();
		    long EndTimeInMs = StartTimeInMs + oneHour;

		    
			    
			    
			    
	        System.out.println("BookAppointment: " + starttime);
	        System.out.println("BookAppointment: " + endtime);
	        System.out.println("BookAppointment: " + myFirstname);
	        System.out.println("BookAppointment: " + mySurname);
	        System.out.println("BookAppointment: " + myEmail);
	        System.out.println("BookAppointment: " + myPhone);
	        
	      //Get configuration data
	        Map<String, String> m = GetConfigurationValues.GetConfigurationValues(AppID);	 
	        GoogleCalendardURL = m.get("GoogleCalendarURL");
	        EmailAddress = m.get("EmailAddress");
	        Password = m.get("Password");
	        
	        /*
            for (AdminValues cg : ConfigurationValues) {
               	GoogleCalendardURL = cg.getGoogleCalendarURL();
               	GoogleEmail = cg.getEmailAddress();
               	Password = cg.getPassword();
              	System.out.println("BookAppointment: GoogleCalendarURL: " + GoogleCalendardURL);
               	System.out.println("BookAppointment: getEmailAddress: " + GoogleEmail);
               	System.out.println("BookAppointment: getPassword: " + Password);
            }*/
            
            	URL feedUrl = new URL(GoogleCalendardURL);
            	CalendarService myService = new CalendarService("exampleCo-exampleApp-1");
            	try {
					myService.setUserCredentials(EmailAddress, Password);
				} catch (AuthenticationException e) {
					e.printStackTrace();
				}		
            	
            	CalendarEventEntry myEntry = new CalendarEventEntry();

            	myEntry.setTitle(new PlainTextConstruct(myFirstname + " " + mySurname));
            	myEntry.setContent(new PlainTextConstruct("E-Mail: " + myEmail + "\nTelefon: " + myPhone));
            	
            	com.google.gdata.data.DateTime startTime = new com.google.gdata.data.DateTime(StartTimeInMs);
            	com.google.gdata.data.DateTime endTime = new com.google.gdata.data.DateTime(EndTimeInMs);
            	When eventTimes = new When();
            	eventTimes.setStartTime(startTime);
            	eventTimes.setEndTime(endTime);
            	myEntry.addTime(eventTimes);


            	// Send the request and receive the response:
            	try {
					CalendarEventEntry insertedEntry = myService.insert(feedUrl, myEntry);
				} catch (ServiceException e) {
					e.printStackTrace();
				}
				resp.sendRedirect("/Calendar.jsp?AppID="+AppID);

	   }
}